package cn.u313.listener;

import cn.u313.comm.conststring.成语;
import cn.u313.comm.utils.Util;
import cn.u313.service.ChengYuService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.MsgSender;
import love.forte.simbot.filter.MatchType;
import me.kuku.utils.OkHttpUtils;
import okhttp3.Headers;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class ETMtPlayListen extends BaseListen {
    @Autowired
    ChengYuService chengYuService;
    Map<String,String> stringMap = new HashMap<>();
    Map<String,String> daAnMap = new HashMap<>();
    @OnGroup
    @Filter(value = "猜成语",matchType = MatchType.EQUALS)
    public  void mean(GroupMsg groupMsg, MsgSender msgSender) throws IOException {

        Map<String,String> map = new HashMap<>();
        map.put("text", 成语.getCHengYu());
        Response response = OkHttpUtils.post("https://emojidaquan.com/Transfer/index", map);
        String str= response.body().string();
        String msg = JSONObject.parseObject(str).getString("msg");
        System.out.println(msg+">"+map.get("text"));
        if (stringMap.containsKey(getGroup(groupMsg)))
            stringMap.replace(getGroup(groupMsg),map.get("text"));
        else
            stringMap.put(getGroup(groupMsg),map.get("text"));
        daAnMap.put(map.get("text"),msg);
        if(msg.equals(map.get("text"))){
            mean(groupMsg,msgSender);
            return;
        }

        chengYuService.save(map.get("text"),msg);
        sendMsg(msgSender,groupMsg, msg);
    }
    @OnGroup
    @Filter(at = "2036085374")
    public  void 解析(GroupMsg groupMsg, MsgSender msgSender) throws IOException {
        if (stringMap.containsKey(getGroup(groupMsg))){
            String msg = stringMap.get(getGroup(groupMsg));
            if (msg == null) {
                return;
            }
            System.out.println(msg);
            if (groupMsg.getMsg().contains(msg))
                sendMsg(msgSender,groupMsg, "真棒棒，竟然对了");

        }
    }
    @OnGroup
    @Filter(value = "搜题",matchType = MatchType.CONTAINS)
    public  void st(GroupMsg groupMsg, MsgSender msgSender) throws IOException {
        String msg = groupMsg.getMsg()
                    .replaceAll(":","")
                    .replaceAll(" ","");
        if(msg.length()<3){
            msg="题目无效";
        }else {
            try {
                String ts = OkHttpUtils.get("http://plugin.313u.cn/v1/question/get?question="+msg.substring(2)).body().string();
                JSONArray objects = JSONArray.parseArray(ts);
                for (int i = 0; i < objects.size(); i++) {
                    JSONObject jsonObject = objects.getJSONObject(i);//,"":"错误",""
                    String timu = jsonObject.getString("Topic");
                    String d1 = jsonObject.getString("Answer");
                    String d2 = jsonObject.getString("Correct");
                    msg += "题目："+timu+"\n～～～～～～～～～～～～～\n答案："+d1+d2+"\n————————————\n";
                }


            }catch (Exception e){
                e.printStackTrace();
                msg = e.getMessage();
            }
        }

        sendMsg(msgSender,groupMsg, msg,false);


    }
}
